home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalComboBoxButton.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  157 lines

  1. /*
  2.  * @(#)MetalComboBoxButton.java    1.13 98/04/21
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.plaf.basic.*;
  26. import com.sun.java.swing.*;
  27. import com.sun.java.swing.plaf.*;
  28. import com.sun.java.swing.border.*;
  29. import java.io.Serializable;
  30.  
  31. /**
  32.  * JButton subclass to help out MetalComboBoxUI
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @see MetalComboBoxButton
  42.  * @version 1.13 04/21/98
  43.  * @author Tom Santos
  44.  */
  45. public class MetalComboBoxButton extends JButton {
  46.     protected JComboBox comboBox;
  47.     protected JList listBox;
  48.     protected CellRendererPane rendererPane;
  49.     protected Icon comboIcon;
  50.     protected boolean iconOnly = false;
  51.  
  52.     public final JComboBox getComboBox() { return comboBox;}
  53.     public final void setComboBox( JComboBox cb ) { comboBox = cb;}
  54.  
  55.     public final Icon getComboIcon() { return comboIcon;}
  56.     public final void setComboIcon( Icon i ) { comboIcon = i;}
  57.  
  58.     public final boolean isIconOnly() { return iconOnly;}
  59.     public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
  60.  
  61.     MetalComboBoxButton() {
  62.         super( "" );
  63.         DefaultButtonModel model = new DefaultButtonModel() {
  64.             public void setArmed( boolean armed ) {
  65.                 super.setArmed( isPressed() ? true : armed );
  66.             }
  67.         };
  68.  
  69.         setModel( model );
  70.     }
  71.  
  72.     public MetalComboBoxButton( JComboBox cb, Icon i, 
  73.                                 CellRendererPane pane, JList list ) {
  74.         this();
  75.         comboBox = cb;
  76.         comboIcon = i;
  77.         rendererPane = pane;
  78.         listBox = list;
  79.     }
  80.  
  81.     public MetalComboBoxButton( JComboBox cb, Icon i, boolean onlyIcon,
  82.                                 CellRendererPane pane, JList list ) {
  83.         this( cb, i, pane, list );
  84.         iconOnly = onlyIcon;
  85.     }
  86.  
  87.     public void paintComponent( Graphics g ) {
  88.         // Paint the button as usual
  89.         super.paintComponent( g );
  90.  
  91.         Insets insets = getInsets();
  92.  
  93.         int width = getWidth() - (insets.left + insets.right);
  94.         int height = getHeight() - (insets.top + insets.bottom);
  95.  
  96.         int left = insets.left;
  97.         int top = insets.top;
  98.         int right = left + (width - 1);
  99.         int bottom = top + (height - 1);
  100.  
  101.         int iconWidth = 0;
  102.         int iconLeft = right;
  103.  
  104.         // Paint the icon
  105.         if ( comboIcon != null ) {
  106.             iconWidth = comboIcon.getIconWidth();
  107.             int iconHeight = comboIcon.getIconHeight();
  108.             int iconTop = 0;
  109.  
  110.             if ( iconOnly ) {
  111.                 iconLeft = (getWidth() / 2) - (iconWidth / 2);
  112.                 iconTop = (getHeight() / 2) - (iconHeight / 2);
  113.             }
  114.             else {
  115.                 iconLeft = (left + (width - 1)) - iconWidth;
  116.                 iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
  117.             }
  118.  
  119.             comboIcon.paintIcon( this, g, iconLeft, iconTop );
  120.  
  121.             // Paint the focus
  122.             if ( hasFocus() ) {
  123.                 g.setColor( MetalLookAndFeel.getFocusColor() );
  124.                 g.drawRect( left - 1, top - 1, width + 3, height + 1 );
  125.             }
  126.         }
  127.  
  128.         // Let the renderer paint
  129.         if ( ! iconOnly && comboBox != null ) {
  130.             ListCellRenderer renderer = comboBox.getRenderer();
  131.             Component c;
  132.             boolean renderPressed = getModel().isPressed();
  133.             c = renderer.getListCellRendererComponent(listBox,
  134.                                                       comboBox.getSelectedItem(),
  135.                                                       -1,
  136.                                                       renderPressed,
  137.                                                       false);
  138.             c.setFont(rendererPane.getFont());
  139.  
  140.             if ( model.isArmed() && model.isPressed() ) {
  141.                 if ( isOpaque() ) {
  142.                     c.setBackground(UIManager.getColor("Button.pressed"));
  143.                 }
  144.             }
  145.             else {
  146.                 c.setBackground(comboBox.getBackground());
  147.             }
  148.             c.setForeground(comboBox.getForeground());
  149.  
  150.             int cWidth = width - (insets.right + iconWidth);
  151.  
  152.             rendererPane.paintComponent( g, c, this, left, top, cWidth, height );
  153.         }
  154.     }
  155. }
  156.  
  157.